home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / MapVsHashMap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.5 KB  |  55 lines

  1. //: C20:MapVsHashMap.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // The hash_map header is not part of the 
  7. // Standard C++ STL. It is an extension that 
  8. // is only available as part of the SGI STL:
  9. #include <hash_map> 
  10. #include <iostream>
  11. #include <map>
  12. #include <ctime>
  13. using namespace std;
  14.  
  15. int main(){
  16.   hash_map<int, int> hm;
  17.   map<int, int> m;
  18.   clock_t ticks = clock();
  19.   for(int i = 0; i < 100; i++)
  20.     for(int j = 0; j < 1000; j++)
  21.       m.insert(make_pair(j,j));
  22.   cout << "map insertions: " 
  23.     << clock() - ticks << endl;
  24.   ticks = clock();
  25.   for(int i = 0; i < 100; i++)
  26.     for(int j = 0; j < 1000; j++)
  27.       hm.insert(make_pair(j,j));
  28.   cout << "hash_map insertions: " 
  29.     << clock() - ticks << endl;
  30.   ticks = clock();
  31.   for(int i = 0; i < 100; i++)
  32.     for(int j = 0; j < 1000; j++)
  33.       m[j];
  34.   cout << "map::operator[] lookups: " 
  35.     << clock() - ticks << endl;
  36.   ticks = clock();
  37.   for(int i = 0; i < 100; i++)
  38.     for(int j = 0; j < 1000; j++)
  39.       hm[j];
  40.   cout << "hash_map::operator[] lookups: "
  41.     << clock() - ticks << endl;
  42.   ticks = clock();
  43.   for(int i = 0; i < 100; i++)
  44.     for(int j = 0; j < 1000; j++)
  45.       m.find(j);
  46.   cout << "map::find() lookups: "
  47.     << clock() - ticks << endl;
  48.   ticks = clock();
  49.   for(int i = 0; i < 100; i++)
  50.     for(int j = 0; j < 1000; j++)
  51.       hm.find(j);
  52.   cout << "hash_map::find() lookups: " 
  53.     << clock() - ticks << endl;
  54. } ///:~
  55.